<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p>
        <label for="firstname">姓</label>
        <input type="text" id="firstname" v-model="firstname" />
      </p>
      <p>
        <label for="lastname">名</label>
        <input type="text" id="lastname" v-model="lastname" />
      </p>
      <p>姓名：{{ fullname }}</p>
    </div>

    <script src="../Vue.js"></script>
    <script>
      // 优先使用计算属性，要处理异步或者开销较大的时候，就只能使用侦听属性了
      // 计算属性里面是不能使用异步的，当响应式的数据改变了就立马要计算出新结果
      new Vue({
        el: "#app",
        data: {
          firstname: "zhang",
          lastname: "san",
          fullname: "zhangsan",
        },

        watch: {
          firstname(val) {
            this.fullname = val + this.lastname;
          },
          lastname(val) {
            setTimeout(() => {
              this.fullname = this.firstname + val;
            }, 2000);
          },
        },

        // computed: {
        //   fullname() {
        //     return this.firstname + this.lastname;
        //   },
        // },
      });
    </script>
  </body>
</html>
